package com.woniuxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacManagerservice;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/rbacManager")
//@CrossOrigin
//@RequiresAuthentication 需要登录认证
@RequiresRoles({"超级管理员","普通管理员"}) //需要admin的角色才允许登录
public class RbacManagerController {
    @Autowired(required = false)
    RbacManagerservice rbacManagerservice;
    @GetMapping("/findOne/{id}")
    public RbacManager findOne(@PathVariable int id){
        return rbacManagerservice.findOne(id);
    }

    /**
     * 分页查询所有的数据
     * @return
     */
    @PostMapping("/findAll/{startPage}/{pageSize}")
//    @RequiresPermissions(logical=Logical.AND,value = {"perm:update","121231231233"}) //需要manager::findAll的权限才允许登录
    public ResponseEntity<PageInfo<RbacManager>> findAll(@PathVariable("startPage") int startPage,@PathVariable("pageSize") int pageSize,
                                         @RequestBody RbacManager rbacManager){
        PageHelper.startPage(startPage, pageSize);
        System.out.println(rbacManager);
        List<RbacManager> managerList = rbacManagerservice.findAll();
        PageInfo<RbacManager> rbacManagerPageInfo = new PageInfo<>(managerList);

        return  new ResponseEntity(200, "查询成功！", rbacManagerPageInfo);
    }
    /**
     * 分页查询所有的数据,最终版，通过ResponseEntity对返回对象再一次封装！
     * @return
     */
    @GetMapping("/findAllResponse/{startPage}/{pageSize}")
    public ResponseEntity<PageInfo<RbacManager>> findAllRes(@PathVariable("startPage") int startPage, @PathVariable("pageSize") int pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<RbacManager> managerList = rbacManagerservice.findAll();
        PageInfo<RbacManager> rbacManagerPageInfo = new PageInfo<>(managerList);
        return new ResponseEntity(200, "查询成功！", rbacManagerPageInfo);
    }
    @DeleteMapping("/deleteOne/{id}")
    public ResponseEntity<String> deleteOne(@PathVariable("id") int id){
      int i =  rbacManagerservice.delete(id);
      if (i > 0) {
          return new ResponseEntity(200, "删除成功！","成功删除"+i+"条数据" );
      }else {
          return new ResponseEntity(500,"删除失败，服务器维护中","");
      }
    }
    @PostMapping("/addManager")
    public ResponseEntity<String> addOne(@RequestBody RbacManager rbacManager){
      int i =  rbacManagerservice.addOne(rbacManager);
        if (i > 0) {
            return new ResponseEntity(200, "保存成功！","成功保存"+i+"条数据" );
        }else {
            return new ResponseEntity(500,"保存失败，服务器维护中","");
        }
    }
    @PutMapping("/updateManager")
    public ResponseEntity<String> updateOne(@RequestBody RbacManager rbacManager) {
       int i = rbacManagerservice.updateOne(rbacManager);
        if (i > 0) {
            return new ResponseEntity(200, "更新成功！","成功更新"+i+"条数据" );
        }else {
            return new ResponseEntity(500,"更新失败，服务器维护中","");
        }
    }
}
